package com.zksk.admin.service.invoice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.admin.domain.invoice.dto.InvoiceDto;
import com.zksk.admin.domain.invoice.dto.InvoiceRequestDto;
import com.zksk.admin.domain.invoice.vo.InvoiceRequestVo;
import com.zksk.admin.mapper.guarantee.GuaranteeDao;
import com.zksk.admin.mapper.invoice.InvoiceDao;
import com.zksk.admin.mapper.orders.OrdersDao;
import com.zksk.admin.service.invoice.InvoiceService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.mbg.admin.domain.MemberInvoiceRecord;
import com.zksk.mbg.admin.domain.Orders;
import com.zksk.mbg.admin.domain.OrdersGuarantee;
import com.zksk.mbg.admin.mapper.MemberInvoiceRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 发票表 服务类
 * </p>
 *
 * @author Zksk Acmen
 * @since 2022-12-30
 */
@Service
public class InvoiceServiceImpl extends ServiceImpl<MemberInvoiceRecordMapper, MemberInvoiceRecord> implements InvoiceService{

    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private GuaranteeDao guaranteeDao;

    @Override
    public IPage<InvoiceDto> invoiceList(InvoiceRequestVo invoiceRequestVo) {
        IPage<MemberInvoiceRecord> pageConf = new Page<>(invoiceRequestVo.getPageNum(), invoiceRequestVo.getPageSize());
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>()
                .like(StringUtils.isNotEmpty(invoiceRequestVo.getEnumProductName()), "o.enum_product_name", invoiceRequestVo.getEnumProductName()) // 产品枚举名称
                .eq(StringUtils.isNotEmpty(invoiceRequestVo.getOrdersCode()), "o.orders_code", invoiceRequestVo.getOrdersCode()) // 订单编号
                .eq(invoiceRequestVo.getStatus() != null, "i.status", invoiceRequestVo.getStatus()) // 开票状态
                .like(StringUtils.isNotEmpty(invoiceRequestVo.getStationName()), "o.stations_name", invoiceRequestVo.getStationName()) // 所属平台
                .like(StringUtils.isNotEmpty(invoiceRequestVo.getOrgName()), "o.orgs_name", invoiceRequestVo.getOrgName()) // 所属机构
                .like(StringUtils.isNotEmpty(invoiceRequestVo.getCompanyName()), "o.member_name", invoiceRequestVo.getCompanyName()) // 企业名称
                .between(invoiceRequestVo.getApplyTimeL() != null && invoiceRequestVo.getApplyTimeR() != null, "i.create_time", invoiceRequestVo.getApplyTimeL(), invoiceRequestVo.getApplyTimeR()) // 申请时间
                .last("and i.del_flag='0' order by i.create_time desc");

        return invoiceDao.invoiceList(pageConf, queryWrapper);
    }

    @Override
    public R<InvoiceRequestDto> selectById(Long id) {
        InvoiceRequestDto invoiceRequestDto = new InvoiceRequestDto();
        //根据发票记录id查询发票信息
        MemberInvoiceRecord memberInvoiceRecord = invoiceDao.selectById(id);
        //根据发票记录id查询订单信息
        Long ordersId = memberInvoiceRecord.getOrdersId();
        Orders orders = ordersDao.selectById(ordersId);
        //根据订单id查询保函信息
        QueryWrapper<OrdersGuarantee> queryWrapper = new QueryWrapper<OrdersGuarantee>()
                .eq("orders_id", orders.getOrdersId());
        OrdersGuarantee ordersGuarantee = guaranteeDao.selectOne(queryWrapper);
        invoiceRequestDto.setMemberInvoiceRecord(memberInvoiceRecord);
        invoiceRequestDto.setOrders(orders);
        invoiceRequestDto.setOrdersGuarantee(ordersGuarantee);
        return R.ok(invoiceRequestDto,"查询发票信息成功");
    }
}
